#include <iostream>
#include "lmanagerview_console_impl.h"
#include "lmanager.h"
#include "lmanagerservice.h"
#include "lmanagerservice_impl.h"
#include "tools.h"
#include <getch.h>
using namespace std;
using namespace MC;

//构造函数
LmanagerviewConsoleImpl::LmanagerviewConsoleImpl(void)
{
	service = new LmanagerServiceImpl;
}
//界面
void LmanagerviewConsoleImpl::menu(void)
{
	while(1)
	{
		system("clear");
		cout << "\e[33m*******欢迎来到图书管理系统*******\e[0m" << endl;
		cout << "\e[33m       1、添加新管理员\e[0m" << endl;
		cout << "\e[33m       2、删除管理员  \e[0m" << endl;
		cout << "\e[33m       3、列出所有管理员名单\e[0m" << endl;
		cout << "\e[33m       4、退出root\e[0m" << endl;
		cout << "\e[33m*********************************\e[0m" << endl;
		cout << "\e[34m请输入指令：\e[0m";
		char cmd = 0;
		while(1)
		{
			cmd = getch();
			if(cmd >='1' && cmd <='4')
				break;
		}
		cout << cmd << endl;
		system("clear");
		switch(cmd-'0')
		{
			case 1:add();break;
			case 2:del();break;
			case 3:list();break;
			case 4:return;
		}
	}	
}
//添加管理员
void LmanagerviewConsoleImpl::add(void)
{
	int m_id = generator_MgrId();
	char m_name[20];
	char m_password[20];
	cout << "\e[34m请输入管理员名：\e[0m" << endl;
	cin >> m_name;
	cout << "\e[34m请输入密码：\e[0m" << endl;
	cin >> m_password;
	Lmanager man(m_id++,m_name,m_password);
	
	if(service->addManager(man))
	{
		cout << "\e[36m管理员添加成功！\e[0m" << endl;
	}
	else
	{
		cout << "\e[31m添加失败!\e[0m" << endl;
	}
	anykey_continue();

}
//删除管理员
void LmanagerviewConsoleImpl::del(void)
{
	int _id = generator_MgrId();
	cout << "\e[34m请输入要删除管理员ID：\e[0m" << endl;
	cin >> _id;
	if(service->deleteManager(_id))
	{
		cout << "\e[34m删除管理员成功！\e[0m" << endl;
	}
	else
	{
		cout << "\e[31m删除失败!\e[0m" << endl;
		
	}
	anykey_continue();

}
void LmanagerviewConsoleImpl::list(void)
{
	service->listManager();
	anykey_continue();
}
LmanagerviewConsoleImpl::~LmanagerviewConsoleImpl(void)
{
	delete []service;
}
